package com.atguigu.yygh.controller;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.service.IHospitalSetService;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author kabuda
 * @create 2023-02-19 15:26
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private IHospitalSetService hospitalSetService;

    @ApiOperation("查找全部医院信息")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation("根据id删除医院信息")
    @DeleteMapping("{id}")
    public Result deleteHospitalSet(@PathVariable("id") Integer id) {
        boolean b = hospitalSetService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("分页条件查询")
    @PostMapping("/findHospitalSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findHospitalSet(@PathVariable("current") Integer current, @PathVariable("limit") Integer limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(hospitalSetQueryVo.getHosname()), HospitalSet::getHosname, hospitalSetQueryVo.getHosname());
        queryWrapper.eq(StringUtils.hasText(hospitalSetQueryVo.getHoscode()), HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());

        Page<HospitalSet> result = hospitalSetService.page(page, queryWrapper);
        return Result.ok(result);
    }

    @ApiOperation("新增医院信息")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置使用状态 0不可 1可
        hospitalSet.setStatus(1);
        //设置签名密钥
        Random random = new Random();
        String encode = MD5Encoder.encode((System.currentTimeMillis() + "" + random.nextInt(1000)).getBytes());
        hospitalSet.setSignKey(encode);

        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();

        }
    }
    @ApiOperation("根据id查询医院信息")
    @GetMapping("/getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable Long id) {
        try {
            HospitalSet hospitalSet = hospitalSetService.getById(id);
            return Result.ok(hospitalSet);
        }catch (Exception e){
            throw new YyghException("发生错误",201);
        }
    }

    @ApiOperation("更新医院信息")
    @PutMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean result = hospitalSetService.updateById(hospitalSet);
        if (result) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    @ApiOperation("批量删除医院信息")
    @DeleteMapping("/batchDeleteHospital")
    public Result batchDeleteHospital(@RequestBody List<Long> ids) {
        boolean b = hospitalSetService.removeByIds(ids);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    @ApiOperation("锁解状态")
    @PutMapping("/lockHospital/{id}/{status}")
    public Result lockHospital(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    @ApiOperation("发送信息")
    @GetMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 后续发送短信

        return Result.ok();
    }


}
